package com.kalix.dataPlatform.right.biz;

import com.kalix.dataPlatform.right.api.biz.IAllNetworkDeviceListBeanService;
import com.kalix.dataPlatform.right.api.dao.IAllNetworkDeviceListBeanDao;
import com.kalix.dataPlatform.right.api.model.HttpRequestUtil;
import com.kalix.dataPlatform.right.entities.AllNetworkDeviceListBean;
import com.kalix.framework.core.impl.biz.GenericBizServiceImpl;
import com.kalix.framework.core.util.ConfigUtil;
import org.json.JSONArray;
import org.json.JSONObject;

public class AllNetworkDeviceListBeanServiceImpl extends GenericBizServiceImpl<IAllNetworkDeviceListBeanDao, AllNetworkDeviceListBean> implements IAllNetworkDeviceListBeanService {
    public static String Ip = (String) ConfigUtil.getConfigProp("FREEBOARD_IP","config.ksh.portconf");
    public static String Port =(String) ConfigUtil.getConfigProp("FREEBOARD_PORT","config.ksh.portconf");
    @Override
    public String getAllNetworkDeviceListValue() {
        String url = "http://"+Ip+":"+Port+"/query/mo/network";
        JSONArray data= HttpRequestUtil.getXpath(url);
        JSONArray all = new JSONArray();
        for(int i= 0;i<data.length();i++){
            JSONArray array = new JSONArray();
            JSONObject obj=data.getJSONObject(i);
            JSONArray qualifiers = obj.getJSONArray("qualifiers");
            for(int j= 0;j<qualifiers.length();j++){
                if(qualifiers.getJSONObject(j).get("name").equals("name")){
                    JSONObject objValue = qualifiers.getJSONObject(j).getJSONObject("value");
                    String valueDisplayName = objValue.getString("value");
                    array.put(valueDisplayName);
                }
            }
            JSONArray properties = obj.getJSONArray("properties");
            for(int k= 0;k<properties.length();k++){
                if(properties.getJSONObject(k).get("name").equals("ip")){
                    JSONObject objValue = properties.getJSONObject(k).getJSONObject("value");
                    String valueIp = objValue.getString("value");
                    array.put(valueIp);
                }else if(properties.getJSONObject(k).get("name").equals("accessStatus")){
                    JSONObject objValue = properties.getJSONObject(k).getJSONObject("value");
                    String valueAccessStatus = objValue.getString("value");
                    array.put(valueAccessStatus);
                }
            }
            all.put(array);
        }
        return all.toString();
    }
}
